package com.max.gateway.config;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClientSpecification;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.stereotype.Component;

/**
 * @author max
 * @date 2024年08月23日 19:41
 */
@Component
public class LoadBalancerClientFactoryPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean.getClass().isAssignableFrom(LoadBalancerClientFactory.class)) {
            List<LoadBalancerClientSpecification> allConfigurations = new ArrayList(((LoadBalancerClientFactory) bean).getConfigurations().values());
            allConfigurations.add(new LoadBalancerClientSpecification("backend-server", new Class[]{ CustomLoadBalancerAutoConfiguration.class }));
            ((LoadBalancerClientFactory) bean).setConfigurations(allConfigurations);
        }
        return bean;
    }
}
